/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.group.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;


import net.comm.group.service.impl.GFollForumServiceImpl;
import net.comm.group.service.impl.GForumServiceImpl;
import net.comm.group.service.impl.GMemberServiceImpl;
import net.comm.group.service.impl.GroupsServiceImpl;
import net.comm.group.web.form.GFollForumForm;
import net.comm.group.web.form.GForumForm;
import net.comm.group.web.form.GMemberForm;
import net.comm.util.HtmlMessageBox;
import net.comm.util.StringUtil;
import net.comm.web.base.AbstractBaseAction;


/**
 *群跟帖话题Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GFollForumAction extends AbstractBaseAction {
	
	private GFollForumServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String LIST_FORWARD = "list";
	final String OPER_FORWARD = "oper";
	
	
	/**
	 *构造方法
	 */
	public GFollForumAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new GFollForumServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(LIST_FORWARD);
		} else {
			return null;
		}
	}
	
	/**
	 * 查询群跟帖话题清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GFollForumForm theForm = (GFollForumForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();

		/**
		 *调用services中的方法
		 */
		List list = service.list(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("followlist" , list);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());

		if(theForm.getPage().getCurPage() == 1){
			//取帖子内容
			GForumForm forumForm = new GForumForm();
			
			forumForm.setGForumID(theForm.getQryGForumID());
			
			GForumServiceImpl forumservice = new GForumServiceImpl();
			
			forumservice.get(forumForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
			{
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
				return showMsg(hmb , request , response , mapping);
			}
			theForm.setQryGroupID(forumForm.getGroupID());
			theForm.setQryVillageID(forumForm.getVillageID());
			request.setAttribute("forumForm", forumForm);
		}
		
		//判断当前用户是否已经加入了该群组
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);

		String addflg = "no";
		if(userID != null){
			GMemberServiceImpl memservice = new GMemberServiceImpl();
			GMemberForm memForm = new GMemberForm();
			
			memForm.setGroupID(theForm.getQryGroupID());
			memForm.setUserID(userID);
			
			memservice.get(memForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS ){
				if(msg.getMsg().equals("会员不存在"))
				{
					request.setAttribute("addflg", addflg);
				}
			}else{
				//当前用户类型（群组创建者或者管理员）
				request.setAttribute("memType", memForm.getType());
				addflg = "yes";
				request.setAttribute("addflg", addflg);
			}
			msg.setErrCode(GlbFinal.ERRCODE_SUCCESS);	
			
		}else{
			
			request.setAttribute("addflg", addflg);
		}		
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	
	/**
	 *添加群跟帖话题
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GFollForumForm theForm = (GFollForumForm) form;
		ReturnMessage msg  = new ReturnMessage();

		//判断群 私密设置和用户是否已经加入，
		
		//判断是否灌水
		String lajireply = StringUtil.getValue((String)request.getSession().getAttribute(GlbFinal.LAJIREPLY));
		if(lajireply.equals("")|| !lajireply.trim().equals(theForm.getContent().trim())){
			
			request.getSession().setAttribute(GlbFinal.LAJIREPLY, theForm.getContent().trim());
		}else if(lajireply.trim().equals(theForm.getContent().trim())){
			
			msg.setMsg("请不要发送重复信息！");
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_PRINTTOHTML);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		service.add(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		request.setAttribute("theForm", theForm);
		
		return list(mapping,form,request,response);

	}

	/**
	 *修改群跟帖话题
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GFollForumForm theForm = (GFollForumForm) form;
		

		ReturnMessage msg  = new ReturnMessage();
		
		service.update(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("theForm", theForm);
		
		return mapping.findForward(LIST_FORWARD);

	}
	/**
	 *删除常见问题
	 */
	public ActionForward del(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GFollForumForm theForm = (GFollForumForm) form;

		ReturnMessage msg  = new ReturnMessage();
		
		service.delete(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}else{
			msg.setMsg("成功删除回复信息");
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_PRINTTOHTML);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl(GlbFinal.FOLDER_INSTALL + "/gfollforum.do?action=list&qryGForumID=" 
					+ theForm.getGForumID());
			return showMsg(hmb , request , response , mapping);
		}
	}
	
}
